//============================================================================
// Name        : CrimInfoAgentBased.cpp
// Author      : Ramyaa Ramyaa, Suresh Marru
// Version     :
//Description : Criminology, Agent based Modelling Proof of Concept
//============================================================================

#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;

bool ret=0;//return with probability of money
bool crime=0;
bool memory=0;


float grid[201][201][3];
int homex = 101, homey = 101;
int curx = 101, cury=101;
int money = 1000, max_money = 1000, min_money=10;
int cost = 1, profit=20;
int iter = 100;
int dir;
int gohome=1000;
ofstream myfile;

void roam_around();
void go_home();
void commit_crime();
void remember();

int main()
{

  myfile.open ("criminal.txt");
 // myfile << "Writing this to a file.\n";

  for (int i=0; i<201; i++)
    for (int j=0; j<201; j++)
      {grid[i][j][0] = 0.5; grid[i][j][1]=50; grid[i][j][2]=4;}

  for (int i=0; i<iter; i++)
   {
     while ((money>=min_money) && (gohome>=money))
      {
         roam_around();
         if (ret) gohome=rand()%1000;
      }
     go_home();
    cout<<i<<" ";
   }

   myfile.close();
   return 0;

}



void roam_around()
{
  dir = rand() % 4;
  if (dir==0) curx++;
  else if (dir==1) curx--;
  else if (dir==2) cury++;
  else cury--;
   // cout<<"here";
   myfile << curx<<" "<<cury<<"\n";

  money-=cost;
   if (crime) commit_crime();
   if (memory) remember();
}



void go_home()
{
   while (curx>homex) {curx--; if (memory) remember();myfile << curx<<" "<<cury<<"\n";}
   while (curx<homex) {curx++; if (memory) remember();myfile << curx<<" "<<cury<<"\n";}
   while (cury>homey) {cury--; if (memory) remember();myfile << curx<<" "<<cury<<"\n";}
   while (cury<homey) {cury++; if (memory) remember();myfile << curx<<" "<<cury<<"\n";}
  money=max_money;
}

void commit_crime()
{
   if (rand()%100 > grid[curx][cury][1])
   {
     cout<<"crime"<<curx<<" "<<cury<<"=n";
     money+=profit;
   }
}



void remember()
{
   for (int i=0; i<201; i++)
    for (int j=0; j<201; j++)
      grid[i][j][1] -= grid[i][j][2];

    for (int i=-2; i<=2; i++)
      for (int j=-2; j<=2; j++)
      {
         if (grid[curx+i][curx+j][1])  grid[curx+i][curx+j][2] -= 2;
         grid[curx+i][curx+j][1] += 15;
      }
}
